#include <sys/types.h>  /* for type definitions */
#include <sys/socket.h> /* for socket API calls */
#include <netinet/in.h> /* for address structs */
#include <arpa/inet.h>  /* for sockaddr_in */
#include <stdio.h>      /* for printf() and fprintf() */
#include <stdlib.h>     /* for atoi() */
#include <string.h>     /* for strlen() */
#include <unistd.h>     /* for close() */

#define MAXBUFFSIZE 1500
/*
 *
 */
int main(int argc, char** argv)
{
    int status, sock, socklen;
    char buffer[MAXBUFFSIZE];
    struct sockaddr_in saddr;
    struct ip_mreq imreq;


    //set content of sockaddr and imreq to zeroes
    memset(&saddr, 0, sizeof(struct sockaddr_in));
    memset(&imreq, 0, sizeof(struct ip_mreq));

    //open a UDP socket
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    if(sock < 0)
        perror("Error creating socket."), exit(0);

    saddr.sin_family = PF_INET;
    saddr.sin_port = htons(4096);               //listen on port 4096
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);  //bind socket to any interface
    status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));

    if(status < 0)
        perror("Error binding socket to interface"), exit(0);

    imreq.imr_multiaddr.s_addr = inet_addr("192.168.0.104");
    imreq.imr_interface.s_addr = INADDR_ANY;

    //join multicast group on default interface
    status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&imreq, sizeof(struct ip_mreq));

    socklen = sizeof(struct sockaddr_in);

    while(1)
    {
        status = recvfrom(sock, buffer, MAXBUFFSIZE, 0, (struct sockaddr *)&saddr, &socklen);
        printf("\n%s", buffer);
    }

    shutdown(sock, 2);
    close(sock);

    return (EXIT_SUCCESS);
}
